﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using EnergyConsumptionMonitorSystem.Components;
using System.Windows;
using EnergyConsumptionMonitorSystem.Models;

namespace EnergyConsumptionMonitorSystem.MainWindowTabItem
{
    public class PersonalInfoUCViewModel : TabItemContentUCViewModel
    {

        private bool _sex = true;
        public bool Sex
        {
            get { return this._sex; }
            set
            {
                if (this._sex != value)
                {
                    this._sex = value;
                    NotifyPropertyChanged("Sex");
                }
            }
        }

        private string _age="";
        public string Age
        {
            get { return this._age; }
            set
            {
                if (this._age != value)
                {
                    this._age = value;
                    NotifyPropertyChanged("Age");
                }
            }
        }

        private string _height="";
        public string Height
        {
            get { return this._height; }
            set
            {
                if (this._height != value)
                {
                    this._height = value;
                    NotifyPropertyChanged("Height");
                }
            }
        }

        private string _weight="";
        public string Weight
        {
            get { return this._weight; }
            set
            {
                if (this._weight != value)
                {
                    this._weight = value;
                    NotifyPropertyChanged("Weight");
                }
            }
        }

        private string _weightRange;
        public string WeightRange
        {
            get { return this._weightRange; }
            set
            {
                if (this._weightRange != value)
                {
                    this._weightRange = value;
                    NotifyPropertyChanged("WeightRange");
                }
            }
        }

        private string _standardWeight;
        public string StandardWeight
        {
            get { return this._standardWeight; }
            set
            {
                if (this._standardWeight != value)
                {
                    this._standardWeight = value;
                    NotifyPropertyChanged("StandardWeight");
                }
            }
        }

        private string _bmi;
        public string BMI
        {
            get { return this._bmi; }
            set
            {
                if (this._bmi != value)
                {
                    this._bmi = value;
                    NotifyPropertyChanged("BMI");
                }
            }
        }

        private string _bmr;
        public string BMR
        {
            get { return this._bmr; }
            set
            {
                if (this._bmr != value)
                {
                    this._bmr = value;
                    NotifyPropertyChanged("BMR");
                }
            }
        }

        private bool _isShowed = false;
        public bool IsShowed
        {
            get { return this._isShowed; }
            set
            {
                if (this._isShowed != value)
                {
                    this._isShowed = value;
                    NotifyPropertyChanged("IsShowed");
                }
            }
        }      

        private ICommand _confirmCommand;
        public ICommand ConfirmCommand { get { return this._confirmCommand; } }      

        public PersonalInfoUCViewModel()
        {
            this._view = new PersonalInfoUC();
            this._view.DataContext = this;

            this._confirmCommand = new Command(new Action<object>(this.Confirm));
            UserInfo userInfo = Singleton<DBManager>.Instance.GetUserInfo(Singleton<LogInStatus>.Instance.User.UserID);
            if (userInfo != null)
            {
                this.Age = userInfo.Age.ToString();
                this.Sex = userInfo.Sex;
                this.Weight = userInfo.Weight.ToString();
                this.Height = userInfo.Height.ToString();
                Confirm(null);
            }

        }

        private void Confirm(object o)
        {
            if (this.Age != "" && this.Height != "" && this.Weight != "")
            {
                int age = int.Parse(this.Age);
                double stature = double.Parse(this.Height);
                double weight = double.Parse(this.Weight);
                double rangeMin = 18 * stature * stature / 10000.0;
                double rangeMax = 24 * stature * stature / 10000.0;
                double bmi = weight / stature / stature * 10000.0;
                double standard,bmr;
                if (this.Sex)
                {
                    standard = (stature - 80.0) * 0.7;
                    bmr = 66.0 + 13.7 * weight + 5.0 * stature - 6.8 * age;
                }
                else
                {
                    standard = (stature - 70.0) * 0.6;
                    bmr = 655.0 + 9.6 * weight + 1.8 * stature - 4.7 * age;
                }
                this.WeightRange = Math.Round(rangeMin, 1, MidpointRounding.AwayFromZero) + " - " + Math.Round(rangeMax, 1, MidpointRounding.AwayFromZero);
                this.StandardWeight = Math.Round(standard, 1, MidpointRounding.AwayFromZero).ToString();
                this.BMI = Math.Round(bmi, 1, MidpointRounding.AwayFromZero).ToString();
                this.BMR = Math.Round(bmr, 1, MidpointRounding.AwayFromZero).ToString();
                this.IsShowed = true;
                UserInfo userInfo = new UserInfo(this.Sex,int.Parse(this.Age),double.Parse(this.Weight),double.Parse(this.Height));
                Singleton<DBManager>.Instance.UpdateUserInfo(Singleton<LogInStatus>.Instance.User.UserID,userInfo);
            }
            else
            {
                MessageBox.Show("请填写完整个人资料！", "错误");
                this.IsShowed = false;
            }
        }

        protected override void tabItem_ClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = false;
        }
    }
}
